package com.example.wac.dto;

import java.io.Serializable;
import java.util.List;

/**
 * @author wuancheng
 * @description
 * @date 2023-05-31 18:07:00
 */

public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -3883676418991065147L;

    /**
     * 查询结果列表
     */
    private List<T> records;

    /** 总记录数 */
    private Long total;

    /** 当前页码 */
    private Long current;

    /** 每页大小 */
    private Long size;

    public static <T> PageResult<T> build(List<T> list, Number current, Number size, Number total) {
        PageResult<T> page = new PageResult<>();
        page.setRecords(list);
        page.setCurrent(current.longValue());
        page.setSize(size.longValue());
        page.setTotal(total.longValue());
        return page;
    }

    /**
     * 设置总页数
     *
     * @return 总页数
     */
    public Long getPages() {
        if (total == null || size == null) {
            return 0L;
        }
        long currSize = size <= 0 ? 1 : size;
        if (total % currSize > 0) {
            return total / currSize + 1;
        }
        return total / currSize;

    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getCurrent() {
        return current;
    }

    public void setCurrent(Long current) {
        this.current = current;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

}

